package com.yanqu.road.dao.impl.activity.shushan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shushan.ShuShanUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2022/3/17 21:26
 */
public class ShuShanUserDataDaoImpl extends TempDataObjectDao<ShuShanUserData> {
    public static String getShuShanUserDataSql = "select * from `t_u_activity_shushan_user_data` where `activityId`=? ";
    public List<ShuShanUserData> getShuShanUserDataList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ShuShanUserData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuShanUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateShuShanUserDataSql = "update `t_u_activity_shushan_user_data` set partyName=?,takeNameTime=?,prestige=?,maxMagic=?,getTimes=?,getRefreshTime=?,buyGetTimes=?,buyGetRefreshTime=?,eventRefreshTime=?,randomDiscipleNum=?,canReceiveEventId=?,growEventFinishMap=?,exitTime=?,receiveGuideReward=?,goodExerciseTimes=?,discipleList=?,unTriggerQualityMap=?,offLineExerciseInfoMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ShuShanUserData shuShanUserData) {
        shuShanUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, shuShanUserData.getPartyName()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getTakeNameTime()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getMaxMagic()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGetTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGetRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getBuyGetTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getBuyGetRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getEventRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getRandomDiscipleNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getCanReceiveEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getGrowEventFinishMap())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getExitTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuShanUserData.getReceiveGuideReward()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGoodExerciseTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getDiscipleList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getUnTriggerQualityMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getOffLineExerciseInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getUserId()));
        if (getDb().executeNoneQuery(updateShuShanUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuShanUserDataSql = "insert into `t_u_activity_shushan_user_data`(activityId,userId,partyName,takeNameTime,prestige,maxMagic,getTimes,getRefreshTime,buyGetTimes,buyGetRefreshTime,eventRefreshTime,randomDiscipleNum,canReceiveEventId,growEventFinishMap,exitTime,receiveGuideReward,goodExerciseTimes,discipleList,unTriggerQualityMap,offLineExerciseInfoMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShuShanUserData shuShanUserData) {
        shuShanUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, shuShanUserData.getPartyName()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getTakeNameTime()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getPrestige()));
        para.put(i++, new DbParameter(Types.BIGINT, shuShanUserData.getMaxMagic()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGetTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGetRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getBuyGetTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getBuyGetRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getEventRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getRandomDiscipleNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getCanReceiveEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getGrowEventFinishMap())));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getExitTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, shuShanUserData.getReceiveGuideReward()));
        para.put(i++, new DbParameter(Types.INTEGER, shuShanUserData.getGoodExerciseTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getDiscipleList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getUnTriggerQualityMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuShanUserData.getOffLineExerciseInfoMap())));
        if (getDb().executeNoneQuery(insertShuShanUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuShanUserData parseData(ResultSet rs) throws SQLException {
        ShuShanUserData shuShanUserData = new ShuShanUserData();
        shuShanUserData.setActivityId(rs.getInt("activityId"));
        shuShanUserData.setUserId(rs.getLong("userId"));
        shuShanUserData.setPartyName(rs.getString("partyName"));
        shuShanUserData.setTakeNameTime(rs.getInt("takeNameTime"));
        shuShanUserData.setPrestige(rs.getLong("prestige"));
        shuShanUserData.setMaxMagic(rs.getLong("maxMagic"));
        shuShanUserData.setGetTimes(rs.getInt("getTimes"));
        shuShanUserData.setGetRefreshTime(rs.getInt("getRefreshTime"));
        shuShanUserData.setBuyGetTimes(rs.getInt("buyGetTimes"));
        shuShanUserData.setBuyGetRefreshTime(rs.getInt("buyGetRefreshTime"));
        shuShanUserData.setEventRefreshTime(rs.getInt("eventRefreshTime"));
        shuShanUserData.setRandomDiscipleNum(rs.getInt("randomDiscipleNum"));
        shuShanUserData.setCanReceiveEventId(rs.getInt("canReceiveEventId"));
        shuShanUserData.setGrowEventFinishMap(JSON.parseObject(rs.getString("growEventFinishMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        shuShanUserData.setExitTime(rs.getInt("exitTime"));
        shuShanUserData.setReceiveGuideReward(rs.getBoolean("receiveGuideReward"));
        shuShanUserData.setGoodExerciseTimes(rs.getInt("goodExerciseTimes"));
        shuShanUserData.setDiscipleList(JSON.parseObject(rs.getString("discipleList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.shushan.ShuShanDiscipleBaseInfo>>() {}));
        shuShanUserData.setUnTriggerQualityMap(JSON.parseObject(rs.getString("unTriggerQualityMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
        shuShanUserData.setOffLineExerciseInfoMap(JSON.parseObject(rs.getString("offLineExerciseInfoMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shushan.ShuShanExerciseInfo>>() {}));

        return shuShanUserData;
    }

}
